package com.ljx.itbbs.server.user.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.common.domain.entity.UserEntity;
import com.ljx.itbbs.server.user.domain.vo.req.UserPageReq;
import com.ljx.itbbs.server.user.domain.vo.req.UserUpdateReq;
import com.ljx.itbbs.server.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

@Service
public class UserDao extends ServiceImpl<UserMapper, UserEntity> {

    public UserEntity selectByUsername(String username) {
        return lambdaQuery().eq(UserEntity::getUsername, username)
                .one();
    }

    public boolean checkUsernameUnique(String username) {
        return lambdaQuery().eq(UserEntity::getUsername, username).count() == 0;
    }

    public void updateUserInfo(Long uid, UserUpdateReq req) {
        this.baseMapper.updateUserInfo(uid, req);
    }

    public void increaseUserIntegral(Long userId, Integer integral) {
        this.baseMapper.increaseUserIntegral(userId, integral);
    }

    public IPage<UserEntity> getUserPage(UserPageReq req) {
        IPage<UserEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        // 条件判断
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();

        return this.baseMapper.selectPage(page, wrapper);

    }

    public void updateUserStatus(Long userId, Integer status) {
        lambdaUpdate().set(UserEntity::getStatus, status)
                .eq(UserEntity::getId, userId)
                .update();
    }
}
